package cn.iocoder.yudao.module.worker.service.gift;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.worker.controller.app.gift.vo.AppWorkerGiftPageReqVO;
import cn.iocoder.yudao.module.worker.controller.app.gift.vo.AppWorkerGiftSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.worker.controller.admin.gift.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.gift.WorkerGiftDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.worker.dal.mysql.gift.WorkerGiftMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.worker.enums.ErrorCodeConstants.*;

/**
 * 礼物 Service 实现类
 *
 * @author Jerry
 */
@Service
@Validated
public class WorkerGiftServiceImpl implements WorkerGiftService {

    @Resource
    private WorkerGiftMapper giftMapper;

    @Override
    public Long createGift(WorkerGiftSaveReqVO createReqVO) {
        // 插入
        WorkerGiftDO gift = BeanUtils.toBean(createReqVO, WorkerGiftDO.class);
        giftMapper.insert(gift);
        // 返回
        return gift.getId();
    }

    @Override
    public void updateGift(WorkerGiftSaveReqVO updateReqVO) {
        // 校验存在
        validateGiftExists(updateReqVO.getId());
        // 更新
        WorkerGiftDO updateObj = BeanUtils.toBean(updateReqVO, WorkerGiftDO.class);
        giftMapper.updateById(updateObj);
    }

    @Override
    public void deleteGift(Long id) {
        // 校验存在
        validateGiftExists(id);
        // 删除
        giftMapper.deleteById(id);
    }

    private void validateGiftExists(Long id) {
        if (giftMapper.selectById(id) == null) {
            throw exception(GIFT_NOT_EXISTS);
        }
    }

    @Override
    public WorkerGiftDO getGift(Long id) {
        return giftMapper.selectById(id);
    }

    @Override
    public PageResult<WorkerGiftDO> getGiftPage(WorkerGiftPageReqVO pageReqVO) {
        return giftMapper.selectPage(pageReqVO);
    }

    @Override
    public Long createGift(AppWorkerGiftSaveReqVO createReqVO) {
        // 插入
        WorkerGiftDO gift = BeanUtils.toBean(createReqVO, WorkerGiftDO.class);
        giftMapper.insert(gift);
        // 返回
        return gift.getId();
    }

    @Override
    public void updateGift(AppWorkerGiftSaveReqVO updateReqVO) {
        // 校验存在
        validateGiftExists(updateReqVO.getId());
        // 更新
        WorkerGiftDO updateObj = BeanUtils.toBean(updateReqVO, WorkerGiftDO.class);
        giftMapper.updateById(updateObj);
    }

    @Override
    public PageResult<WorkerGiftDO> getGiftPage(AppWorkerGiftPageReqVO pageReqVO) {
        return giftMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WorkerGiftDO> getGiftList() {
        return giftMapper.getGiftList();
    }

    @Override
    public List<WorkerGiftDO> getGiftList(Set<Long> ids) {
        if(CollectionUtils.isAnyEmpty(ids)){
            return CollectionUtils.newArrayList(new ArrayList<>());
        }
        return giftMapper.selectBatchIds(ids);
    }

    @Override
    public List<WorkerGiftDO> getAllGiftList() {
        return giftMapper.getAllGiftList();
    }

}